/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package exercises.day03.pt02;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 *
 * @author Student
 */
public class AuthorDao {
    //STEP 2: Create connection

    private String url = "jdbc:derby://localhost:1527/webshoppe;user=root;password=root";
    private Connection conn;

    public AuthorDao() throws ClassNotFoundException, SQLException {
        //set driver
        Class.forName("org.apache.derby.jdbc.ClientDriver");
        //open connection
        conn = DriverManager.getConnection(url);
    }

    public boolean addAuthor(Author author) throws SQLException {
        PreparedStatement ps = conn.prepareStatement("INSERT INTO ROOT.AUTHORS (AID, ANAME, AAGE, ACOUNTRY, AMAIL, APHONE) VALUES (?,?,?,?,?,?)");
        ps.setString(1, author.getId());
        ps.setString(2, author.getName());
        ps.setInt(3, author.getAge());
        ps.setString(4, author.getCountry());
        ps.setString(5, author.getMail());
        ps.setString(6, author.getPhone());

        return ps.executeUpdate() != 0 ? true : false;
    }
}
